package com.jds.test.jdk;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Created by sunyuming on 18/8/1.
 */
class ServerHandler4 extends SimpleChannelInboundHandler<BaseBean> {

    //用于记录次数
    private int count = 0;
    //读取客户端发送的数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BaseBean msg) throws Exception {
        System.out.println("RESPONSE--------"+msg.toString());
        ctx.writeAndFlush(msg);

        if(msg.getNo().equals(5)) {
            BaseBean baseBean = new BaseBean();
            baseBean.setRandom("共有"+Server4.CHANNEL_GROUP.size()+"个客户端接收广播");
            Server4.CHANNEL_GROUP.writeAndFlush(baseBean);
        }

    }

    //新客户端接入
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
        Server4.CHANNEL_GROUP.add(ctx.channel());
    }

    //客户端断开
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
    }

    //异常
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //关闭通道
        ctx.channel().close();
        //打印异常
        cause.printStackTrace();
    }
}